package com.stadium.controller;

import com.stadium.common.BaseResponse;
import com.stadium.dto.RRuleDetailDto;
import com.stadium.service.RRuleDetailSV;
import com.stadium.vo.RRuleDetailVo;
import com.stadium.vo.searchVo.RRuleDetailSearchVo;
import io.micrometer.core.instrument.util.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 规则细则
 *
 * @author zq
 * @email 513973172@qq.com
 * @date 2020-01-31 15:13:39
 */
@RestController
@RequestMapping("/api/rruledetail")
@Api(tags = "规则细则")
public class RRuleDetailCL {
    private Logger logger = LoggerFactory.getLogger(this.getClass());
    @Autowired
    private RRuleDetailSV rRuleDetailSV;

    @ApiOperation(value = "保存或修改")
    @PostMapping(value = "/v1/save")
    public BaseResponse save(@Validated RRuleDetailVo rRuleDetailVo) {
        String id = "";
        String message = "";
        try {
            if (rRuleDetailVo.getId() == null || rRuleDetailVo.getId() == "") {
                rRuleDetailSV.save(rRuleDetailVo);
                message = "添加成功";
            } else {
                rRuleDetailSV.update(rRuleDetailVo);
                message = "修改成功";
            }
            return BaseResponse.ok(message);

        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            return BaseResponse.error();
        }
    }

    @ApiOperation(value = "删除")
    @PostMapping(value = "/v1/delete")
    @ApiImplicitParams({@ApiImplicitParam(name = "id", value = "id")})
    public BaseResponse delete(@RequestParam(name = "id", required = true) String id) {
        try {
            rRuleDetailSV.delete(id);
            return BaseResponse.ok();
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            return BaseResponse.error();
        }
    }

    @ApiOperation(value = "批量删除")
    @PostMapping(value = "/v1/deleteBatch")
    @ApiImplicitParams({@ApiImplicitParam(name = "ids", value = "ids")})
    public BaseResponse deleteBatch(@RequestParam(name = "ids", required = true) String[] ids) {
        try {
            rRuleDetailSV.deleteBatch(ids);
            return BaseResponse.ok();
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            return BaseResponse.error();
        }
    }

    @ApiOperation(value = "获得列表")
    @GetMapping(value = "/v1/query")
    public BaseResponse<List<RRuleDetailDto>> query(@Validated RRuleDetailSearchVo searchVo) {
        try {
            if (StringUtils.isNotEmpty(searchVo.getSearchValue())) {
                searchVo.setSearchValue("%" + searchVo.getSearchValue() + "%");
            }
            List<RRuleDetailDto> queryList = rRuleDetailSV.query(searchVo);
            int total = rRuleDetailSV.getCount(searchVo);
            return BaseResponse.ok(queryList, total);
        } catch (Exception e) {
            logger.error("日志失败！", e);
            return BaseResponse.error();
        }
    }

    @ApiOperation(value = "获得详情", notes = "获得详 情")
    @GetMapping(value = "/v1/get")
    @ApiImplicitParams({@ApiImplicitParam(name = "id", value = "id")})
    public BaseResponse<RRuleDetailDto> get(@RequestParam(name = "id", required = true) String id) {
        try {
            RRuleDetailDto queryData = rRuleDetailSV.get(id);
            return BaseResponse.ok(queryData);
        } catch (Exception e) {
            logger.error("日志失败！", e);
            return BaseResponse.error();
        }
    }
}
